<template>
  <div class="login">
    <!-- <van-nav-bar @click-left="$router.push('/home/find')">
      <template #left>
        <i class="iconfont f44">&#xe637;</i>
      </template>
    </van-nav-bar> -->
    <hmNavBar path="/home/find"></hmNavBar>

    <div class="content">
      <h3 class="title">您好，请登录</h3>
      <van-form ref="form">
        <van-field
          v-model="form.mobile"
          name="mobile"
          placeholder="请输入手机号"
          :rules="rules.mobile"
        >
          <template #left-icon> <i class="iconfont">&#xe60b;</i> </template>
        </van-field>
        <van-field
          v-model="form.code"
          placeholder="请输入验证码"
          :rules="rules.code"
        >
          <template #left-icon>
            <i class="iconfont">&#xe60c;</i>
          </template>
          <template #button>
            <span class="code" @click="getCode" v-if="totalTime === 6">
              获取验证码
            </span>
            <span class="code" @click="getCode" v-else
              >{{ totalTime }}
              S后重试
            </span>
          </template>
        </van-field>
      </van-form>
      <div class="tip">
        登录即同意<span class="color">《用户使用协议》</span>和 和<span
          class="color"
          >《隐私协议》</span
        >
      </div>
      <van-button block round color="#e40137" @click="submit">确定</van-button>
    </div>
  </div>
</template>

<script>
import { auCode, auLogin } from '@/api/login.js'
// 导入工具型方法用于token处理
// 如果不写括号会报object(..)处理
import { setLocal, getLocal } from '@/utils/local.js'
export default {
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      totalTime: 6, // 倒计时时间
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'onChange' },
          {
            validator: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '请输入正确的手机号',
            trigger: 'onChange'
          }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'onChange' }]
      }
    }
  },
  created () {
    // window.console.log(process.env.VUE_APP_URL)
    // 如果有token直接跳转到home
    if (getLocal('token')) {
      this.$router.push('/home')
    }
  },
  methods: {
    // 获取验证码
    getCode () {
      this.$refs.form
        .validate('mobile')
        .then(() => {
          //   this.$toast.success('验证成功')
          // 调用api获取验证码
          /*
        axios.get
        axios.post
        axios({
           method:"请求方式默认是get",
           url:"接口地址?参数名=值",
           params:{
              参数名:值(get请求参数) 
           },
           data:{
               参数名:值(post请求)
           }
        })
        
        */
          // this.$toast.success('验证成功')
          //倒计时处理
          this.totalTime-- //防止在倒计时的时候重复点击 6秒内 一开始就是负数 就会点击不了
          const _interval = setInterval(() => {
            this.totalTime--
            if (this.totalTime <= 0) {
              // 清除计时器
              clearInterval(_interval)
              // 还原时间
              this.totalTime = 6
            }
          }, 1000)

          /*  this.$axios({
            method: 'post',
            url: '/au/login',
            data: {
              mobile: this.form.mobile
            }
          }) */
          auCode({
            mobile: this.form.mobile
          })
            .then(res => {
              window.console.log(res)
              this.$toast.success(res.data.data)
              this.form.code = res.data.data
            })
            .catch(error => {
              window.console.log(error)
            })
        })
        .catch(() => {
          this.$toast.success('验证失败')
        })
    },
    submit () {
      // async与await
      /*
      1:去掉.then 在,then前面的方法加一个await  
      2:定义一个变量(为.then的返回值)=await 方法,没有返回值,不用
      */
      this.$refs.form
        .validate()
        .then(() => {
          //   this.$toast.success('验证成功')
          this.$toast.loading('加载中')
          auLogin(this.form)
            .then(res => {
              // this.$toast.success('登录成功')
              window.console.log(res)
              // 保存tocken
              setLocal('token', res.data.data.jwt)
              // 储存用户信息
              this.$store.commit('setUserInfo', res.data.data.user)
              // 设置登录状态码为true
              this.$store.commit('setLoginState', true)
              //清除$toast
              this.$toast.clear()
              // 从哪来回哪去
              const _redirect = this.$route.query.redirect
              if (_redirect) {
                this.$router.push(_redirect)
              } else {
                // 跳转
                this.$router.push('/home')
              }
            })
            .catch(err => {
              window.console.log(err)
            })
        })
        .catch(() => {
          this.$toast.fail('验证失败')
        })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  .f44 {
    font-size: 44px;
  }
  .content {
    padding: 0 @p15;
    .title {
      margin-top: 50px;
      margin-bottom: 63px;
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
    }
    .code {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #00b8d4;
      line-height: 22px;
      letter-spacing: 0px;
    }
    .tip {
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #181a39;
      line-height: 16px;
      letter-spacing: 0px;
      margin-bottom: 44px;
      .color {
        color: #00b8d4;
      }
    }
  }
}
</style>
